// simple fragment shader

// 'time' contains seconds since the program was linked.
uniform sampler2D sampler;

uniform float centerX;
uniform float centerY;
uniform float centerZ;

uniform float sRepeat;
uniform float tRepeat;

varying vec3 vertex;
varying vec3 normal;

uniform bool selected;



vec2 getST() {
    float pi = 3.1415;


    vec2 st;
	
   	vec3 r = normalize(vertex - vec3(centerX, centerY, centerZ));
    
   	float alpha = asin(r.y);
   	float beta= atan(r.x,r.z);
    
   	st.s = beta/(2.0*pi);
   	st.t = alpha/pi+0.5;
    
    return st;
 }
 


void main()
{
	vec2 index = getST();
	index.s = index.s * sRepeat;
	index.t = index.t * tRepeat;
	vec4 color = texture2D(sampler, index);
	if (!selected) color -= 0.3;
	//if (selected && normal.z < 0.1) color = gl_Color;
	//gl_FragColor = vec4(1.0/pow(distance(center,vertex),2),0.0,0.0,0.0);
	
	gl_FragColor = color;
}


